<template>
  <section class="mobile-audit-page">
    <div class="body-box">
      <p class="name-box">
        <span class="name-label">姓名</span>
        <span class="name-value">{{ userInfo.studentName }}</span>
      </p>
      <div
        v-if="userInfo.remark && userInfo.status == '2'"
        class="status-box"
      >
        <img class="status-icon" src="@/assets/images/user/unauth.png">
          审核不通过：{{userInfo.remark}}
      </div>
      <van-form ref="dataForm" :scroll-to-error="true" validate-trigger="onSubmit">
        <div style="width:100%;padding: 0 0 20px">
            <van-field
            v-model="studentStr"
            is-link
            readonly
            label="学生类型"
            placeholder=""
            style="padding:0"
            @click="showPicker = true"
            />
            <van-popup v-model:show="showPicker" round position="bottom">
            <van-picker
                v-model="studentType"
                title="学生类型"
                show-toolbar
                :columns="columns"
                @cancel="showPicker = false"
                @confirm="onConfirm"
            />
            </van-popup>
        </div>
        <div v-if="isChina" class="card-box">
          <p class="card-title">{{studentAbroad === 1 ? '身份证信息' : '护照信息'}}</p>
          <div class="card-num-item" v-if="studentAbroad === 1">
            <van-field
              v-model="cardNo"
              required
              label="身份证号码"
              placeholder="请输入您的身份证号码"
              :maxlength="18"
              :rules="[{ message: '', validator: validataNum }]"
            />
          </div>
          <div class="card-num-item" v-if="studentAbroad === 2">
            <van-field
              v-model="passport"
              required
              label="护照编号"
              placeholder="请输入您的护照编号"
              :maxlength="18"
              :rules="[{ message: '', validator: validataNum }]"
            />
          </div>
          <span class="card-line"></span>
          <div class="card-img-list">
            <div class="card-img-item">
              <div class="card-img-wrap" @click="uploadImg( 'uploadNumOne')">
                <img class="card-img-default" :style="{opacity
                : frontPic2 ? 0 : 1}" src="@/assets/images/mobile/img-upload.png" />
                <img v-if="frontPic2" class="card-img-upload" :src="frontPic2" />
                <span v-if="frontPic2" class="card-img-delete" @click.stop="deleteImg('frontPic2')">
                  <van-icon class="card-img-delete-icon" name="cross" />
                </span>
              </div>
              <p class="card-img-tip">请上传{{ studentAbroad === 1 ? '身份证' : '护照' }}正面照片</p>
            </div>
            <div class="card-img-item">
              <div class="card-img-wrap" @click="uploadImg( 'uploadNumTwo')">
                <img class="card-img-default" :style="{opacity
                : endPic2 ? 0 : 1}" src="@/assets/images/mobile/img-upload.png" />
                <img v-if="endPic2" class="card-img-upload" :src="endPic2" />
                <span v-if="endPic2" class="card-img-delete" @click.stop="deleteImg('endPic2')">
                  <van-icon class="card-img-delete-icon" name="cross" />
                </span>
              </div>
              <p class="card-img-tip">请上传{{ studentAbroad === 1 ? '身份证' : '护照' }}反面照片</p>
            </div>
          </div>
        </div>
        <div v-if="!isChina">
          <div class="card-num-item">
            <van-field
              v-model="email"
              required
              label="认证邮箱:"
              placeholder="请输入您的邮箱地址"
              :rules="[{ message: '', validator: validataEmail }]"
            />
          </div>
          <span class="card-line"></span>
          <p class="card-email-tip">
            (请确认您的邮箱地址，我们将通过该邮箱进行邀请认证，请输入本学校学生邮箱）
          </p>
        </div>
        <div class="card-box">
          <p class="card-title">学生证信息</p>
          <p class="card-tip">在校生请上传学生证，毕业生请上传毕业证</p>
          <div class="card-img-list">
            <div class="card-img-item">
              <div class="card-img-wrap" @click="uploadImg( 'uploadOne')">
                <img class="card-img-default" :style="{opacity
                : frontPic ? 0 : 1}" src="@/assets/images/mobile/img-upload.png" />
                <img v-if="frontPic" class="card-img-upload" :src="frontPic" />
                <span v-if="frontPic" class="card-img-delete" @click.stop="deleteImg('frontPic')">
                  <van-icon class="card-img-delete-icon" name="cross" />
                </span>
              </div>
              <p class="card-img-tip">
                认证材料正面
                <img class="card-img-tip-icon" src="@/assets/images/mobile/tip.png" />
              </p>
            </div>
            <div class="card-img-item">
              <div class="card-img-wrap" @click="uploadImg( 'uploadTwo')">
                <img class="card-img-default" :style="{opacity
                : endPic ? 0 : 1}" src="@/assets/images/mobile/img-upload.png" />
                <img v-if="endPic" class="card-img-upload" :src="endPic" />
                <span v-if="endPic" class="card-img-delete" @click.stop="deleteImg('endPic')">
                  <van-icon class="card-img-delete-icon" name="cross" />
                </span>
              </div>
              <p class="card-img-tip">
                认证材料反面
                <img class="card-img-tip-icon" src="@/assets/images/mobile/tip.png" />
              </p>
            </div>
          </div>
        </div>
      </van-form>
    </div>
    <div class="btn-box">
      <van-button
        type="info"
        class="btn-item"
        :class="{'disabled': !canSave}"
        @click="onSubmit()"
      >
        点击上传
      </van-button>
    </div>
  </section>
</template>

<script>
export default {
  // 组件名称
  name: "",
  // 组件参数 接收来自父组件的数据
  props: {
    userInfo: Object,
    isChina: Boolean | String,
    cardNoNew: String,
    frontPic2: String,
    endPic2: String,
    frontPic: String,
    endPic: String,
  },
  // 局部注册的组件
  components: {

   },
  // 组件状态值
  data() {
    return {
      studentAbroad: 1,
      studentType: '1',
      columns:[
        { text: '中国内地学生', value: '1' },
        { text: '非中国内地学生', value: '2' },
      ],
      passport: '',
      showPicker: false,
      cardNo: '',
      email: '',
    };
  },
  watch: {
    cardNoNew(val) {
      this.cardNo = val
    },
    userInfo: {
      handler(obj) {
        this.email = obj.email || ''
      },
      deep: true
    }
  },
  computed: {
    studentStr () {
        return this.studentAbroad === 1 ? '中国内地学生' : '非中国内地学生'
    },
    canSave() {
      return this.frontPic !== '' || this.endPic !== ''
    }
  },
  created() {
  },
  // 组件方法
  methods: {
    onConfirm({value}) {
      this.studentAbroad = Number(value)
      this.showPicker = false
    },
    validataNum(value) {
      return value !== ""
    },
    validataEmail(value) {
      return new Promise((resolve) => {
        let emailReg =
        /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
        if (value === "") {
          return resolve(false);;
        } else {
          if (emailReg.test(value)) {
            return resolve(true);
          } else {
            return resolve(false);;
          }
        }
      });
    },
    onSubmit() {
      if (!this.canSave) return
      this.$refs.dataForm.validate().then(() => {
        console.log('save')
        this.$emit('submit', {
          cardNo: this.cardNo,
          email: this.email,
          studentAbroad: this.studentAbroad,
          passport: this.passport
        })
      });
    },
    uploadImg(key) {
      this.$emit('uploadImg' ,{ key })
    },
    deleteImg(key) {
      this.$emit('deleteImg' ,{ key })
    }
  },
  beforeDestroy() {
  }
};
</script>
<style lang="less">
.van-button--info {
  border: 1px solid #4677B9;
  background-color: #4677B9;
}
</style>
<style lang="less" scoped>
.mobile-audit-page {
  display: none;
  padding: 0.15rem 0.1rem 0.75rem;

  .name-box {
    margin-bottom: 0.2rem;
    padding-left: 0.02rem;
    font-size: 0.15rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #37383D;
    line-height: 0.21rem;
  }
  .name-label {
    margin-right: 0.15rem;
    color: #9597A0;
  }

  .status-box {
    display: inline-flex;
    margin-bottom: 0.2rem;
    padding: 0.03rem 0.09rem 0.03rem 0.08rem;
    align-items: center;
    font-size: 0.13rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #FE4E3D;
    line-height: 0.18rem;
    border-radius: 0.04rem;
    border: 0.01rem solid #FE4E3D;
    background: rgba(254,78,61,0.1);
  }
  .status-icon {
    margin-right: 0.05rem;
    width: 0.11rem;
    height: 0.13rem;
  }

  .card-box {
    & + .card-box {
      margin-top: 0.3rem;
    }
  }
  .card-title {
    font-size: 0.16rem;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #37383D;
    line-height: 0.22rem;
  }
  .card-num-item {
    margin-top: 0.08rem;
    ::v-deep .van-cell {
      padding: 0.13rem 0 0.13rem 0.11rem;
      line-height: 0.46rem;
      &.van-cell--required::before {
        left: 0;
        font-size: 0.16rem;
        transform: translateY(3px);
      }
      .van-field__label {
        margin-right: 0;
        width: 1rem;
      }
    }
  }
  .card-line {
    display: block;
    width: 100%;
    height: 1px;
    background: #E6E6E6;
    opacity: 0.6;
  }
  .card-email-tip {
    margin: 0.1rem 0 0.25rem;
    font-size: 0.12rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #9597A0;
    line-height: 0.17rem;
  }
  .card-tip {
    margin-top: 0.07rem;
    font-size: 0.12rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #9597A0;
    line-height: 0.17rem;
  }
  .card-img-list {
    display: flex;
    margin-top: 0.15rem;
  }
  .card-img-item {
    flex: 1;
    & + .card-img-item {
      margin-left: 0.11rem;
    }
  }
  .card-img-wrap {
    position: relative;
    width: 100%;
    border-radius: 0.05rem;
    overflow: hidden;
  }
  .card-img-default {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .card-img-upload {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  .card-img-delete {
    position: absolute;
    top: 0;
    right: 0;
    width: 0.25rem;
    height: 0.25rem;
    background-color: rgba(0, 0, 0, 0.7);
    border-radius: 0 0.06rem 0 0.25rem;
  }
  .card-img-delete-icon {
    position: absolute;
    top: 0;
    right: 0;
    color: #fff;
    font-size: 0.2rem;
    transform: scale(0.7);
  }
  .card-img-tip {
    display: flex;
    margin-top: 0.1rem;
    align-items: center;
    font-size: 0.12rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #37383D;
    line-height: 0.17rem;
    justify-content: center;
  }
  .card-img-tip-icon {
    margin-left: 0.06rem;
    width: 0.14rem;
    height: 0.14rem;
  }
  ::v-deep .van-uploader {
    .van-uploader__wrapper {
      display: block;
    }
  }

  .btn-box {
    display: flex;
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 1;
    padding: 0 0.15rem;
    width: 100%;
    height: 0.75rem;
    align-items: center;
    background-color: #fff;
  }
  .btn-item {
    flex: 1;
    height: 0.55rem;
    font-size: 0.18rem;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    line-height: 0.25rem;
    border-radius: 0.05rem;
    &.disabled {
      background-color: #C8C8C8;
      border-color: #C8C8C8;
    }
  }
}
</style>
